/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entities.Plano;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

/**
 *
* @author Sostenes, Thiago, Felipe
 */
@Stateless
public class PlanoDao implements PlanoDaoLocal {

    @PersistenceContext(unitName = "SSGH-ejbPU", type = PersistenceContextType.TRANSACTION)
    private EntityManager em;

    @Override
    public void create(Plano plano) {
        em.persist(plano);
    }

    @Override
    public boolean exists(Plano plano) {
        Query query = em.createQuery("SELECT p FROM Plano p WHERE p.nome = ?1");
        query.setParameter(1, plano.getNome());
        return !(query.getResultList().isEmpty());
    }

    @Override
    public void remove(Plano plano) {
        if (!em.contains(plano)) {
            plano = em.find(plano.getClass(), plano.getId());
        }
        em.remove(plano);
    }

    @Override
    public List<Plano> list() {
        Query query = em.createQuery("SELECT p FROM Plano p");
        return query.getResultList();
    }
}
